//
//  YZLSearchCarView.m
//  Car
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 岳明. All rights reserved.
//

#import "YZLSearchCarView.h"
#import "YZLSubBrandListCell.h"
#import "YZLSeriesListCell.h"
#import "YZLSeriesListModel.h"
#import "YZLSubBrandListCell.h"
#import "YZLSearchModel.h"
#import "Macro.h"
#import "HTTPTOOL.h"
#import "YZLCarListModel.h"
#import "YZLSubBrandListModel.h"
#import "YZLSearchCarSecondView.h"
#import "YZLHeaderCell.h"

@interface YZLSearchCarView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) YZLSearchModel *searchModel;
@property (nonatomic, strong) YZLSearchCarSecondView *secondView;
@property (nonatomic, strong) UIImageView *searchingImageView;
@property (nonatomic, strong) UIImageView *searchNullImageView; /**< 空的 */

@end

@implementation YZLSearchCarView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 117) style:UITableViewStylePlain];
        [self addSubview:self.tableView];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        
        [self.tableView registerClass:[YZLSeriesListCell class] forCellReuseIdentifier:@"seriesListReuse"];
        [self.tableView registerClass:[YZLSubBrandListCell class] forCellReuseIdentifier:@"subReuse"];
        [self.tableView registerClass:[YZLHeaderCell class] forCellReuseIdentifier:@"headerReuse"];
        
        self.searchingImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"searching"]];
        [self addSubview:self.searchingImageView];
        self.searchingImageView.frame = CGRectMake(0, 0, kWidth, kHeight - 117);

    }
    return self;
}

- (void)setBodyString:(NSString *)bodyString {
    if (_bodyString != bodyString) {
        _bodyString = bodyString;
    }
    [self handleDateWithBodyString:bodyString];
    
}

#pragma mark - 数据解析
- (void)handleDateWithBodyString:(NSString *)bodyString {
    NSString *transcodingString = [bodyString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet  URLQueryAllowedCharacterSet]];
    NSString *str = [NSString stringWithFormat:@"searchContent=%@&", transcodingString];
    [HTTPTOOL POSTWithURL:searchCar body:str bodyStyle:stringStyle httpHead:nil responseStyle:JSON success:^(id result) {
        self.searchModel = [YZLSearchModel yzlSearchModelWithDic:result];
        if ([self.searchModel.resultType integerValue] == 1) {
            NSArray *seriesArray = [result objectForKey:@"seriesList"];
            NSMutableArray *mSeriesArray = [NSMutableArray array];
            for (NSDictionary *dic in seriesArray) {
                YZLSeriesListModel *seriesModel = [YZLSeriesListModel yzlSeriesListModelWithDic:dic];
                NSArray *carArray = [dic objectForKey:@"carList"];
                NSMutableArray *mCarArray = [NSMutableArray array];
                for (NSDictionary *dic in carArray) {
                    YZLCarListModel *carModel = [YZLCarListModel yzlCarListModelWithDic:dic];
                    [mCarArray addObject:carModel];
                }
                seriesModel.carList = mCarArray;
                [mSeriesArray addObject:seriesModel];
            }
            self.searchModel.seriesList = mSeriesArray;
        } else {
            NSArray *subArray = [result objectForKey:@"subBrandList"];
            NSMutableArray *mSubArray = [NSMutableArray array];
            for (NSDictionary *dic in subArray) {
                YZLSubBrandListModel *subModel = [YZLSubBrandListModel yzlSubBrandListModelWithDic:dic];
                NSArray *seriesArray = [dic objectForKey:@"seriesList"];
                NSMutableArray *mSeriesArray = [NSMutableArray array];
                for (NSDictionary *dic in seriesArray) {
                    YZLSeriesListModel *seriesModel = [YZLSeriesListModel yzlSeriesListModelWithDic:dic];
                    [mSeriesArray addObject:seriesModel];
                }
                subModel.seriesList = mSeriesArray;
                [mSubArray addObject:subModel];
            }
            self.searchModel.subBrandList = mSubArray;
        }
        
        [_tableView reloadData];
        [self.searchingImageView removeFromSuperview];
        
        if (self.searchModel.seriesList.count == 0 && self.searchModel.subBrandList.count == 0) {
            self.searchNullImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"searchNull"]];
            [self addSubview:self.searchNullImageView];
            self.searchNullImageView.frame = CGRectMake(0, 0, kWidth, kHeight - 117);
        }
        
    } fail:^(NSError *error) {

    }];
}

#pragma mark - 每组几行
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if ([self.searchModel.resultType integerValue] == 1) {
        YZLSeriesListModel *serisesModel = [self.searchModel.seriesList objectAtIndex:section];
        return serisesModel.carList.count + 1;
    } else {
        YZLSubBrandListModel *subModel = [self.searchModel.subBrandList objectAtIndex:section];
        return subModel.seriesList.count;
    }
}

#pragma mark - 共有几组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if ([self.searchModel.resultType integerValue] == 1) {
        return self.searchModel.seriesList.count;
    } else {
        return self.searchModel.subBrandList.count;
    }
}

#pragma mark - cell赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.searchModel.resultType integerValue] == 1) {
        YZLSeriesListModel *seriesModel = [self.searchModel.seriesList objectAtIndex:indexPath.section];
        if (indexPath.row == 0) {
            YZLHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"headerReuse"];
            cell.model = seriesModel;
            cell.searchString = self.bodyString;
            return cell;
        } else {
            YZLCarListModel *carModel = [seriesModel.carList objectAtIndex:indexPath.row - 1];
            YZLSeriesListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"seriesListReuse"];
            cell.model = carModel;
            cell.searchString = self.bodyString;
            return cell;
        }
    } else {
        YZLSubBrandListModel *subModel = [self.searchModel.subBrandList objectAtIndex:indexPath.section];
        YZLSeriesListModel *serModel = [subModel.seriesList objectAtIndex:indexPath.row];
        YZLSubBrandListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"subReuse"];
        cell.model = serModel;
        cell.searchString = self.bodyString;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.searchModel.resultType integerValue] == 1 && indexPath.row == 0) {
        return kHeight / 10;
    } else return 50;
}

#pragma mark - 自定义组头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if ([self.searchModel.resultType integerValue] == 2) {
        self.secondView = [[YZLSearchCarSecondView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 30)];
        [self addSubview:self.secondView];
        self.secondView.model = [self.searchModel.subBrandList objectAtIndex:section];
        self.secondView.searchString = self.bodyString;
        return self.secondView;
    } else {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        [self addSubview:view];
        return view;
    };
}



#pragma mark - 组的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if ([self.searchModel.resultType integerValue] == 2) {
        return 30.0;
    } return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if ([self.searchModel.resultType integerValue] == 1) {
        return 15;
    } else return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.delegate pushNextViewOfSearchCarWithIndexPath:indexPath withModel:self.searchModel];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
